package seismosurfer.layer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import seismosurfer.data.MacroData;
import seismosurfer.graphics.SeismoDrawingAttributes;
import seismosurfer.graphics.SeismoPoint;
import seismosurfer.gui.panel.MacroseismicPanel;
import seismosurfer.http.HttpGateway;

import com.bbn.openmap.event.LayerStatusEvent;
import com.bbn.openmap.event.MapMouseEvent;
import com.bbn.openmap.omGraphics.OMGraphic;
import com.bbn.openmap.omGraphics.OMGraphicList;
import com.bbn.openmap.util.SwingWorker;

/**
 * An extension of the SeismoLayer for the 
 * macroseismic data.
 * 
 */
public class MacroseismicLayer extends SeismoLayer {
    
    private static final long serialVersionUID = -1635252756944481107L;

    protected List macrosList = new ArrayList(200);

    public MacroseismicLayer() {
        super();
    }

    public List getMacroDataList() {
        return macrosList;
    }
    
    /**
     * Fetches the macroseismic data related to a quake and 
     * visualizes them on the map through this layer.
     * 
     * @param quakeID the id of the earthquake
     *  
     */
    public void loadMacroseismicForQuake(final long quakeID) {

        final SwingWorker worker = new SwingWorker() {

            public Object construct() {
                fireRequestInfoLine(updateInfoLine);
                fireStatusUpdate(LayerStatusEvent.START_WORKING);

                macrosList.clear();
                macrosList = HttpGateway.getMacroseismicForQuake(quakeID);
                loadGraphics();

                return null;
            }

            // Runs on the event-dispatching thread.
            public void finished() {
                fireRequestInfoLine(emptyInfoLine);
                fireStatusUpdate(LayerStatusEvent.FINISH_WORKING);
            }
        };
        worker.start();

    }

    public void loadGraphics() {

        OMGraphicList gr = getList();

        if (macrosList == null) {
            System.out.println("macrosList == null!!!");
        }

        if (gr == null) {
            gr = new OMGraphicList(macrosList.size());
        } else {
            gr.clear();
        }

        SeismoPoint point;

        for (Iterator i = macrosList.iterator(); i.hasNext();) {
            MacroData md = (MacroData) i.next();

            point = new SeismoPoint(md.getSiteLatitude(), md.getSiteLongitude());
            point.setAppObject(md);

            for (Iterator it = seismoAttributesList.iterator(); it.hasNext();) {
                ((SeismoDrawingAttributes) it.next()).setTo(point);
            }

            gr.add(point);
        }

        setList(gr);

        doPrepare();

    }

    public String getToolTipTextFor(OMGraphic omg) {

        if (getActiveMouseModeID().equals(infoMouseMode)) {
            return "Intensity : "
                    + ((MacroData) omg.getAppObject()).getIntensityChar();

        }

        return null;
    }

    protected MacroseismicPanel attrPanel = null;

    public boolean leftClick(OMGraphic omg, MapMouseEvent mme) {

        if (getActiveMouseModeID().equals(navMouseMode)) {
            getMouseEventInterpreter().rightClick(omg, mme);
        }

        else if (getActiveMouseModeID().equals(infoMouseMode)) {

            if (attrPanel == null) {
                attrPanel = new MacroseismicPanel(getName());
            }
            attrPanel.setAttributes(omg.getAppObject());
            attrPanel.showAttributesDialog(mme.getPoint());

        }

        return true;
    }

    public void findAndInit(Object someObj) {

        super.findAndInit(someObj);

    }

    public void findAndUndo(Object someObj) {
        super.findAndUndo(someObj);
    }

}
